﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.DigitizationDSTableAdapters;

namespace ChapterX.BusinessFacade
{
    public class DigitizationSystem
    {
        private DigitizationDS.DigitizationDataTable _digitizationSchema = null;
        public DigitizationDS.DigitizationDataTable DigitizationSchema
        {
            get
            {
                if (null == _digitizationSchema)
                {
                    _digitizationSchema = new DigitizationDS.DigitizationDataTable();
                }
                return _digitizationSchema;
            }
        }

        private DigitizationDS.DigitizationLogsDataTable _digitizationlogSchema = null;
        public DigitizationDS.DigitizationLogsDataTable DigitizationLogSchema
        {
            get
            {
                if (null == _digitizationlogSchema)
                {
                    _digitizationlogSchema = new DigitizationDS.DigitizationLogsDataTable();
                }
                return _digitizationlogSchema;
            }
        }

        #region Digitization
        public bool SubmitDigitazation(DigitizationDS.DigitizationRow digitization, DateTime submitTime)
        {
            bool result = false;

            try
            {
               
                    #region Handle Books

                DigitizationTableAdapter adapter = new DigitizationTableAdapter();
                DigitizationDS.DigitizationDataTable data = adapter.GetDigitizationByBookId(digitization.BookId);
                if (data.Rows.Count == 0)
                {
                    // 数字化信息不存在，插入
                    result = InsertDigitization(adapter, digitization, submitTime);
                }
                else
                {
                    // 课程信息已存在，得到课程信息
                    // 更新课程信息
                    //result = UpdateCourse(csAdapter, course, submitCenterId);
                }

                #endregion
                
            }
            catch (VerifyException ex)
            {
                throw ex;
            }
            catch (UnauthorizedException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex.Message);
            }

            return result;
        }

        public bool InsertDigitization(DigitizationTableAdapter adapter, DigitizationDS.DigitizationRow digitization,
            DateTime submitTime)
        {
            bool result = false;

            try
            {
                int r = adapter.InsertDigitization(
                    digitization.BookId,
                    (int)CommonDefinition.DigitizationStatus.submitted);

                if (r > 0)
                    return true;
            }
            catch
            {
                result = false;
            }

            return result;
        }

        public bool UpdateDigitizationStatus(int bookId, int digitizationStatus)
        {
            bool result = false;

            try
            {
                DigitizationTableAdapter adapter = new DigitizationTableAdapter();
                DigitizationDS.DigitizationDataTable data = adapter.GetDigitizationByBookId(bookId);

                if(data.Rows.Count > 0){
                    int r = adapter.UpdateDigitizationStatus(bookId, digitizationStatus);

                    if (r > 0)
                        return true;
                }
            }
            catch
            {
                result = false;
            }

            return result;
        }


        public bool UpdateDigitizationStatus(DigitizationTableAdapter adapter, int bookId, int digitizationStatus)
        {
            bool result = false;

            try
            {
                DigitizationDS.DigitizationDataTable data = adapter.GetDigitizationByBookId(bookId);

                if (data.Rows.Count > 0)
                {
                    int r = adapter.UpdateDigitizationStatus(bookId, digitizationStatus);

                    if (r > 0)
                        return true;
                }
            }
            catch
            {
                result = false;
            }

            return result;
        }

        public int GetDigitizationStatus(int bookId)
        {
            Data.DigitizationDSTableAdapters.DigitizationTableAdapter adapter = new ChapterX.Data.DigitizationDSTableAdapters.DigitizationTableAdapter();
            DigitizationDS.DigitizationDataTable table = adapter.GetDigitizationByBookId(bookId);

            if (null != table && table.Rows.Count > 0)
                return table[0].DigitizationStatus;
            else
                return -1;
        }

        #endregion

        #region Digitization Log
        public bool SubmitDigitazationLog(DigitizationDS.DigitizationLogsRow digitizationLog, DateTime submitTime)
        {
            bool result = false;

            try
            {

                #region Handle Books

                DigitizationLogsTableAdapter adapter = new DigitizationLogsTableAdapter();
                DigitizationDS.DigitizationLogsDataTable data = adapter.GetDigitizationLogByBookId(digitizationLog.BookId);
                if (data.Rows.Count == 0)
                {
                    // 数字化信息不存在，插入
                    digitizationLog.DigitizationStatus = (int)CommonDefinition.DigitizationStatus.submitted;
                    result = InsertDigitizationLog(adapter, digitizationLog, submitTime);
                }
                else
                {
                    // 课程信息已存在，得到课程信息
                    // 更新课程信息
                    //result = UpdateCourse(csAdapter, course, submitCenterId);
                }

                #endregion

            }
            catch (VerifyException ex)
            {
                throw ex;
            }
            catch (UnauthorizedException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex.Message);
            }

            return result;
        }

        public bool InsertDigitizationLog(DigitizationLogsTableAdapter adapter, DigitizationDS.DigitizationLogsRow digitizationLog,
            DateTime submitTime)
        {
            bool result = false;

            try
            {
                int r = adapter.InsertDigitizationLogs(
                    digitizationLog.BookId,
                    digitizationLog.DigitizationStatus,
                    digitizationLog.OperatorId,
                    digitizationLog.OperatorName,
                    digitizationLog.Operation,
                    submitTime,
                    digitizationLog.IsDescriptionNull() ? null : digitizationLog.Description);

                if (r > 0)
                    return true;
            }
            catch
            {
                result = false;
            }

            return result;

        }
        #endregion
    }
}
